# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::wxui::widget::Queue;
use strict;
use utf8;
use Wx;
use base 'Wx::Panel';
use Wx::Event qw(EVT_BUTTON);
use Wx qw(wxHORIZONTAL wxVERTICAL wxEXPAND wxALL);
use Wx qw(wxHLB_MULTIPLE);
use Wx::Html;
use Tools;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $parent = shift;
	my $id = shift;

	my $self = $class->SUPER::new($parent, $id, [-1, -1], [-1, -1]);
	$self->{Queue} = shift;
	my $planet = $self->{Queue}->getEmpire()->planets->get(map {$self->{Queue}->getProp($_)} qw(x y));
	my $prod = ($planet->getProp('population') / 100 * (1 + 0));
	my ($peace, $war) = map {$self->{Queue}->getEmpire()->getProp($_)} qw(bonus_build_peace bonus_build_war);

	$self->{Sizer} = Wx::BoxSizer->new(wxVERTICAL);

	$self->{Items} = Wx::SimpleHtmlListBox->new($self, -1, [-1, -1], [-1, -1], [], wxHLB_MULTIPLE);
	$self->{Items}->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$self->{Items}->Clear();
	for (my $i = 0; $i < $self->{Queue}->itemsCount; ++$i)
	{
		my ($id, $class, $done, $tag) = map {$self->{Queue}->itemData($i)->{$_}} qw(id class building_points_done i_tag);
		my $prot = $self->{Queue}->getEmpire()->designHouse()->getPrototypeByID($class);
		my ($name, $need, $laser, $bomb) = map {$prot->getProp($_)} qw(s_name build_speed laser_damage bomb_damage);
		my $bonus = ($laser || $bomb) ? $war : $peace;
		$need = int(100 * $need / (1 + $bonus / 100) + 0.5) / 100;
		$self->{Items}->Append(['<font color="white">'.$name.' ('.$done.' / '.$need.' : '.int($done / $need * 100).'% '.int(($need - $done) / $prod).')</font>']);
		
	}

	$self->{Sizer}->Add($self->{Items}, 1, wxEXPAND, wxALL, 0);

	$self->SetSizer($self->{Sizer});

	return $self;
}

sub getQueue
{
	my $self = shift;
	return $self->{Queue};
}

1;
